package clientApp;
import java.net.*;
import java.io.*;
/*
 * This class has an object that is the client thread, it takes in 
 * a socket and the user's choice to send to the server. It trys to conect to
 * the output stream, records the current time in Milliseconds, sends the command
 * to the server, records the server's responses in a String waiting for a flag
 * that signals the server is done outputting, records the stop time in 
 * milliseconds, calculates the time elapsed, displays the output that the server
 * sent, and then adds the elapse time in main's timeAvg object called times.
 */
public class ThreadClient extends Thread {
   private Socket client = null;
   private PrintWriter outData = null;
   private BufferedReader inData = null;
   private int UserNum;
   Menu mnu = new Menu();    
 
           
   ThreadClient(Socket client, int userChoice) {
     this.client=client;
     UserNum=userChoice;
   }
   
    public void run() {
        try {
            String input, output;
            output="";
            outData = new PrintWriter(client.getOutputStream(), true);
            inData = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));
            System.out.println("Connection established to " + client.getInetAddress());
            Long Start, Stop, time;
            Start=System.currentTimeMillis();
            outData.println(UserNum);
            while((input=inData.readLine())!=null)
            {
                if(input.equals("end")) {
                    break;
                }
                output=output+input+"\n";
            }
            Stop=System.currentTimeMillis();
            time=Stop-Start;
            System.out.println(output);
            //System.out.println(time);
            Main.times.addTime(time);
            //active = true;
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host" + client.getInetAddress());
            System.exit(1);
        }catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                    + "the connection");
            System.exit(1);
        }
    }
}

